package Action;

import Entities.DoeBookEntity;
import Entities.UserEntity;
import Service.RecommendService;
import Service.UpdateInfoService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import java.util.List;
import java.util.Map;

//主页Action，跳转到主页时，得到用户名参数，若session中isLogin为true，这跳转到主页，并返回推荐列表
public class HomeAction extends ActionSupport
{
    private String user_name;

    private List<DoeBookEntity> recommendList;
    private int initLimit = 14;
    private String after_id = "0";
    private String limit=String.valueOf(initLimit);
    private List<DoeBookEntity> feedList;
    //在HomeAction中依次调用recommendService获取推荐列表，调用updateInfoService获取用户标签
    private UpdateInfoService updateInfoService;
    private RecommendService recommendService;

    private String whichPanel = "1";  //用于表示跳转回主页时跳到哪个Panel
    private String tag_register = "0";  //若为0则表示是重新选择标签页面过来的；若为1则表示是注册的选择标签页面过来的

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public RecommendService getRecommendService() {
        return recommendService;
    }

    public void setRecommendService(RecommendService recommendService) {
        this.recommendService = recommendService;
    }

    public List<DoeBookEntity> getRecommendList() {
        return recommendList;
    }

    public void setRecommendList(List<DoeBookEntity> recommendList) {
        this.recommendList = recommendList;
    }

    public int getInitLimit() {
        return initLimit;
    }

    public void setInitLimit(int initLimit) {
        this.initLimit = initLimit;
    }

    public String getAfter_id() {
        return after_id;
    }

    public void setAfter_id(String after_id) {
        this.after_id = after_id;
    }

    public String getLimit() {
        return limit;
    }

    public void setLimit(String limit) {
        this.limit = limit;
    }

    public List<DoeBookEntity> getFeedList() {
        return feedList;
    }

    public void setFeedList(List<DoeBookEntity> feedList) {
        this.feedList = feedList;
    }

    public UpdateInfoService getUpdateInfoService() {
        return updateInfoService;
    }

    public void setUpdateInfoService(UpdateInfoService updateInfoService) {
        this.updateInfoService = updateInfoService;
    }

    public String getWhichPanel() {
        return whichPanel;
    }

    public void setWhichPanel(String whichPanel) {
        this.whichPanel = whichPanel;
    }

    public String getTag_register() {
        return tag_register;
    }

    public void setTag_register(String tag_register) {
        this.tag_register = tag_register;
    }

    //获得推荐列表，并返回到主界面
    public String execute()
    {
         Map<String,Object> session =  ActionContext.getContext().getSession();
         boolean isLogin = (boolean)session.get("isLogin");
         if(isLogin == false)
             return "fail";

         String user_name = (String)session.get("user_name");  //获得用户名

        //接下来返回推荐列表
        String param_after_id = ServletActionContext.getRequest().getParameter("after_id");
        String param_limit = ServletActionContext.getRequest().getParameter("limit");
        String session_Id = ServletActionContext.getRequest().getSession().getId();
        String result = executeRecommend(param_after_id, param_limit, user_name, session_Id);
        session.put("recommendList",recommendList);

        //接下来获取用户标签并返回
        UserEntity ue = updateInfoService.returnUserInfo(user_name);
        if(ue==null)
            return "fail";

        String[] tagList = new String[6];
        tagList[0] = ue.getUserTag1();
        tagList[1] = ue.getUserTag2();
        tagList[2] = ue.getUserTag3();
        tagList[3] = ue.getUserTag4();
        tagList[4] = ue.getUserTag5();
        tagList[5] = ue.getUserTag6();
        session.put("tagList",tagList);

        return result;
    }

    //执行推荐算法，返回推荐图书列表
    public String executeRecommend(String param_after_id, String param_limit, String user_name, String session_Id)
    {
        setAfter_id(param_after_id != null ? param_after_id : "0");
        setLimit(param_limit != null ? param_limit : String.valueOf(initLimit));
        if (Integer.parseInt(after_id) >= initLimit){
            setFeedList(recommendService.recommend(user_name, Integer.parseInt(after_id),Integer.parseInt(limit), session_Id));
            return "append";
        }else {
            setRecommendList(recommendService.recommend(user_name, Integer.parseInt(after_id),Integer.parseInt(limit), session_Id));
            return "success";
        }
    }

}
